package com.ld.admin.service.oa.work;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Date;

import org.springframework.stereotype.Service;

import com.ld.admin.model.oa.work.WeeklyReportModel;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.BasicService;
import com.ld.shieldsb.common.web.StaticParams;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.PageNavigationBean;
import com.ld.shieldsb.dao.model.QueryModel;

/**
 * 
 * 工作周报服务类，@Service注解用于自动注入
 * 
 * @ClassName WeeklyReportService
 * @author <a href="mailto:1769975326@qq.com" target="_blank">黄升军</a>
 * @date 2019年08月15日 17:22:54
 *
 */
@Service
public class WeeklyReportService extends BasicService {

    // 用于子类覆盖，扩展doSearch方法中QueryModel的处理（标准queryModel处理完，查询进行前的处理）
    @Override
    protected QueryModel extendDealQuery(QueryModel queryModel) {
        // TODO Auto-generated method stub
        return super.extendDealQuery(queryModel);
    }

    // 用于子类覆盖，扩展doSearch方法中PageNavigationBean的处理（查询完成后进行）
    @Override
    protected <T> PageNavigationBean<T> extendDealPageNavigationBean(PageNavigationBean<T> pageNavigationBean) {
        // TODO Auto-generated method stub
        return super.extendDealPageNavigationBean(pageNavigationBean);
    }

    // 删除方法子类扩展（删除完成后进行）
    @Override
    protected <T> boolean extendRemove(Boolean flag, Class<T> classOfT, String key, Object value, Integer status, Long userId) {
        // TODO Auto-generated method stub
        return flag;
    }

    // AddOrUpdate扩展（AddOrUpdate方法完成后进行）
    @Override
    protected <T> boolean extendAddOrUpdate(Boolean flag, T modelBean, boolean update, String key, Long userId) {
        // TODO Auto-generated method stub
        return flag;
    }

    /**
     * 获取本周和下周的日期范围
     * 
     * @Title getTimeInterval
     * @author 黄升军
     * @date 2019年8月20日 上午10:00:31
     * @param date
     * @return
     */
    public String getTimeInterval(Date date) {

        LocalDate now = LocalDate.now();
        LocalDate monday = now.with(DayOfWeek.MONDAY);
        LocalDate friday = now.with(DayOfWeek.FRIDAY);
        request.setAttribute("start", monday);// 本周星期一日期
        request.setAttribute("end", friday);// 本周星期五日期
        /*--------------------------*/
        // 下周日期范围
        LocalDate nextWeekDate = now.plusWeeks(1);// 下周
        LocalDate nextMonday = nextWeekDate.with(DayOfWeek.MONDAY);
        LocalDate nextFriday = nextWeekDate.with(DayOfWeek.FRIDAY);
        request.setAttribute("nextStart", nextMonday);// 下周周一日期
        request.setAttribute("nextEnd", nextFriday);// 下周周五日期
        return monday + "," + friday;
    }

    /**
     * 更新或者插入
     * 
     * @Title addOrUpdate
     * @author 黄升军
     * @date 2019年8月21日 上午8:43:22
     * @param model
     * @return
     */
    public Object addOrUpdate(WeeklyReportModel model) {
        String start = "";
        String end = "";
        if (model.getStartDate() != null && model.getEndDate() != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            start = sdf.format(model.getStartDate());// 格式化开始日期
            end = sdf.format(model.getEndDate());// 格式化结束日期
            if (start.compareTo(end) > 0) {
                return Web.Response.error("本周周报的开始时间不能大于结束时间");
            }

            if (model.getNextWeekStartDate() != null && model.getNextWeekStartDate() != null) {
                String nextStart = sdf.format(model.getNextWeekStartDate());
                String nextEnd = sdf.format(model.getNextWeekEndDate());
                if (nextStart.compareTo(nextEnd) > 0) {
                    return Web.Response.error("下周计划的开始时间不能大于结束时间");
                }
            }
        }
        if (StringUtils.isEmpty(model.getId())) {// id为空时
            QueryModel queryModel = new QueryModel();
            queryModel.addLike("startDate", start);
            queryModel.addLike("endDate", end);
            queryModel.addEq("createBy", Web.Util.getSessionUserId(request));
            queryModel.addEq("state", StaticParams.STATE_NORMAL);
            WeeklyReportModel weeklyReport = getBaseDAO().getOne(WeeklyReportModel.class, queryModel);
            if (weeklyReport != null) {// 查询出的结果不为空
                return Web.Response.error("本周周报已存在！请删除之后再添加或者直接修改本周周报");
            }
        }
        Result result = super.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

}
